beta<-read.table("merged_beta.txt",sep="\t",head=T,na.strings=c('-'),colClasses=colClasses)
samples<-colnames(beta)[2:2662]
colnames(beta)<-c("SEGID",seq(1,2661))
sitenames<-c("BREAST","COLON","GLIOBLASTOMA","KIDNEY RENAL CLEAR","KIDNEY RENAL PAPILLARY","BLOOD LEUKEMIA","LUNG ADENOMA","LUNG SQUAMOUS","OVARY","RECTUM","STOMACH","UTERUS")
pdf("test_boxplot.pdf")
i<-1
while(i<=length(names))
{	
	
	min<-min(which(site==names(table(site))[i]))+1
	max<-max(which(site==names(table(site))[i]))+1
	main<-sitenames[i]
	print(main)
	while(min<max)
	{
		print(paste("min",min,sep=" "))
		print(paste("max",max,sep=" "))
		temp<-50
		if(min+temp > max)
		{
			temp<-max-min	
		}
		temp1<-min+temp
		boxplot(beta[min:temp1],horizontal=FALSE,show.names = TRUE,main=main,xlab="SAMPLES",ylab="Beta",ylim=c(0,1))
		min<-min+temp
	}
	i<-i+1
}
dev.off()

##################heatmap of top IQR beta's###################
iqr<-apply(beta[,2:ncol(beta)],1,function(x)IQR(x,na.rm=TRUE))
data<-as.matrix(beta[which(iqr> 0.425),2:ncol(beta)])
#tissue type
site<-c()
i<-1
@a<-c()
while(i<2662)
{
@a=rbind(@a,unlist(strsplit(samples[i], "\\.")))
#site[i]<- unlist(strsplit(samples[i], "\\."))[1]
i<-i+1
}

#tumor/normal
dis<-c()
i<-1
while(i<2662)
{
dis[i]<- unlist(strsplit(samples[i], "\\."))[5]
i<-i+1
}

#replace dis with color codes
dis1<-c()
i<-1
while(i<2662)
{
	if(dis[i] == "11A" || dis[i] == "11B")
	{
		dis1[i]<-2
	}
	else if(dis[i] == "20A")
	{
		dis1[i]<-3
	}
	else
	{
		dis1[i]<-1
	}
	i<-i+1
}
#"black","red","green","blue","cyan","magenta","yellow","gray","violet","orange","white","darkblue"
#replace site with color codes
site[which(site=="BRCA")]<-"black"
site[which(site=="COAD")]<-"red"
site[which(site=="GBM")]<-"green"
site[which(site=="KIRC")]<-"blue"
site[which(site=="KIRP")]<-"cyan"
site[which(site=="LAML")]<-"magenta"
site[which(site=="LUAD")]<-"yellow"
site[which(site=="LUSC")]<-"gray"
site[which(site=="OV")]<-"violet"
site[which(site=="READ")]<-"orange"
site[which(site=="STAD")]<-"white"
site[which(site=="UCEC")]<-"darkblue"
color.matrix<-rbind(site,dis1)
color.matrix<-as.matrix(rbind(site,dis1))
colnames(color.matrix)<-samples
rownames(color.matrix)<-c("TISSUE","COND")
colnames(data)<-samples
rownames(data)<-beta[which(iqr> 0.425),1]

library("heatmap.plus")
library(Hmisc)
library("gplots")
spearman<-rcorr(data,type="spearman")
hc<-hclust(as.dist(1-spearman$r),"ave")
clab<-matrix(as.character(t(color.matrix)),nrow=dim(color.matrix)[2],ncol=dim(color.matrix)[1])
colnames(clab)<-rownames(color.matrix)

pdf("heatmap.pdf")
heatmap(data,ColSideColors=site,main="BETA ACCROSS TISSUES",xlab=c("SAMPLES"),ylab=c("TOP IQR 525 PROBES"),labRow=NA,labCol=NA)
legend("topright",cex=0.4,title="Tissue",fil=c("black","red","green","blue","cyan","magenta","yellow","gray","violet","orange","white","darkblue"),c("BREAST","COLON","GLIOBLASTOMA","KIDNEY RENAL CLEAR","KIDNEY RENAL PAPILLARY","BLOOD LEUKEMIA","LUNG ADENOMA","LUNG SQUAMOUS","OVARY","RECTUM","STOMACH","UTERUS"))
dev.off()
pdf("heatmap_noclust.pdf")
heatmap(data,ColSideColors=site,Colv=NA,main="BETA ACCROSS TISSUES NO COL CLUST",xlab=c("SAMPLES"),ylab=c("TOP IQR 525 PROBES"),labRow=NA,labCol=NA)
legend("topright",cex=0.4,title="Tissue",fil=c("black","red","green","blue","cyan","magenta","yellow","gray","violet","orange","white","darkblue"),c("BREAST","COLON","GLIOBLASTOMA","KIDNEY RENAL CLEAR","KIDNEY RENAL PAPILLARY","BLOOD LEUKEMIA","LUNG ADENOMA","LUNG SQUAMOUS","OVARY","RECTUM","STOMACH","UTERUS"))
dev.off()

